package com.ruoyi.web.controller.applet;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.BdmReceiveAddress;
import com.ruoyi.system.domain.dto.BdmReceiveAddressDto;
import com.ruoyi.system.service.IBdmReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址Controller
 *
 * @author ruoyi
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/applet/address")
public class AppletReceiveAddressController extends BaseController {

    @Autowired
    private IBdmReceiveAddressService bdmReceiveAddressService;

    /**
     * 查询用户地址列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BdmReceiveAddress bdmReceiveAddress) {
        bdmReceiveAddress.setUid(getUserId().toString());
        List<BdmReceiveAddress> list = bdmReceiveAddressService.selectBdmReceiveAddressList(bdmReceiveAddress);
        return getDataTable(list);
    }


    /**
     * 获取用户地址详细信息
     */
    @GetMapping(value = "getAddressById/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(bdmReceiveAddressService.selectBdmReceiveAddressById(id));
    }

    /**
     * 新增用户地址
     */
    @PostMapping("/saveAddress")
    public AjaxResult add(@RequestBody BdmReceiveAddress bdmReceiveAddress) {
        bdmReceiveAddressService.saveAddress(bdmReceiveAddress);
        return success();
    }

    /**
     * 修改用户地址
     */
    @PostMapping("/updateAddress")
    public AjaxResult edit(@RequestBody BdmReceiveAddress bdmReceiveAddress) {
        bdmReceiveAddressService.updateAddress(bdmReceiveAddress);
        return success();
    }

    /**
     * 删除用户地址
     */
    @PostMapping("/deleteAddressIds")
    public AjaxResult remove(@RequestBody BdmReceiveAddressDto dto) {
        return toAjax(bdmReceiveAddressService.deleteBdmReceiveAddressByIds(dto.getIds()));
    }


    /**
     * 查询用户默认收货地址
     */
    @GetMapping("/defaultAddr")
    public AjaxResult one() {
        return success( bdmReceiveAddressService.lambdaQuery().eq(BdmReceiveAddress::getUid, getUserId()).eq(BdmReceiveAddress::getIsDefault, "0").one());
    }
}
